package com.business_clean.ui.activity.address;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.callback.OnDialogConfirmListener;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.uitl.ToastUtil;
import com.business_clean.app.weight.EqualSpacingItemDecoration;
import com.business_clean.data.mode.address.AddressEntity;
import com.business_clean.databinding.ActivityAddressManagerBinding;
import com.business_clean.ui.adapter.address.AddressMangerAdapter;
import com.business_clean.viewmodel.request.AddressViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class AddressManagerActivity extends BaseActivity<AddressViewModel, ActivityAddressManagerBinding> {

    private int page = 1;
    private int requestType;
    private AddressMangerAdapter mAdapter;
    private int isMultiple = 0;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {

        mDatabind.bottom.butBottomButLast.setVisibility(View.GONE);

        if (getIntent() != null && getIntent().getExtras() != null) {
            isMultiple = getIntent().getExtras().getInt("multiple");//0是不选择，1 是单选，2是多选
            if (isMultiple == 2) {
                mDatabind.bottom.butBottomButLast.setVisibility(View.VISIBLE);
                mDatabind.tabbar.setRightText("添加打卡地点");
                mDatabind.bottom.butBottomButNext.setText("确定");
                mDatabind.bottom.butBottomButLast.setText("不限制");
                // 标题类型
                String project_name = getIntent().getExtras().getString("project_name");
                if (!TextUtils.isEmpty(project_name)) {
                    mDatabind.tabbar.setTitle(project_name + "的打卡地点");
                } else {
                    mDatabind.tabbar.setTitle("选择打卡地点");
                }
            } else {
                mDatabind.bottom.butBottomButNext.setText("+ 添加打卡地点");
            }
        } else {
            if (App.appViewModelInstance.getProjectInfo().getValue() != null) {
                mDatabind.tabbar.setTitle(App.appViewModelInstance.getProjectInfo().getValue().getProject_short_name() + "的打卡地点");
            }
            mDatabind.bottom.butBottomButNext.setText("+ 添加打卡地点");
        }
        mAdapter = new AddressMangerAdapter(isMultiple);
        mDatabind.list.recyclerView.setAdapter(mAdapter);
        mDatabind.list.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.list.recyclerView.addItemDecoration(new EqualSpacingItemDecoration(SizeUtils.dp2px(10)));


        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });
        requestOne();
    }

    @Override
    public void onBindViewClick() {

        mDatabind.tabbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityForwardUtil.startActivity(AddAddressActivity.class);
            }
        });

        mDatabind.bottom.butBottomButLast.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                App.appViewModelInstance.getSelectedLimitationAddress().setValue(true);
                finish();
            }
        });

        mDatabind.bottom.butBottomButNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isMultiple == 2) {//只有多选的时候才处理
                    App.appViewModelInstance.getSelectedAddress().setValue(mAdapter.getData());
                    finish();
                    return;
                }
                ActivityForwardUtil.startActivity(AddAddressActivity.class);
            }
        });

        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {
                switch (view.getId()) {
                    case R.id.iv_item_address_del:
                        CommonUtils.showGeneralDialog(AddressManagerActivity.this, "删除地址", "是否删除该地址？",
                                "取消", "确定", null, new OnDialogConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        mViewModel.delAddress(mAdapter.getData().get(position).getUuid());
                                    }
                                });
                        break;
                }
            }
        });
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (isMultiple != 0) {//说明是选择
                    mAdapter.updateItem(position);
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("address", mAdapter.getData().get(position));
                    ActivityForwardUtil.startActivity(PreviewAddressActivity.class, bundle);
                }
            }
        });
    }


    @Override
    public void onRequestSuccess() {
        mViewModel.getDelsStatus().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                requestOne();
            }
        });
        mViewModel.getAddressManager().observe(this, new Observer<AddressEntity>() {
            @Override
            public void onChanged(AddressEntity data) {
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(data.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(data.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(data.getList());
                        }
                        break;
                }
                if (isMultiple == 2) {
                    String uuids = getIntent().getExtras().getString("uuids");
                    //如果是多选的话，地址反选一下
                    mAdapter.dataInvert(uuids);
                }
            }
        });
    }

    @Override
    public void initObserver() {
        App.appViewModelInstance.getRefreshAddressManagerList().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                requestOne();
            }
        });
    }

    private void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.requestAddressAll(page);
    }

    private void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.requestAddressAll(page);
    }
}